﻿// ***********************************************************************
// Assembly         : OSS.Doc
// Author           : tianteng
// Created          : 04-28-2020
//
// Last Modified By : tianteng
// Last Modified On : 04-29-2020
// ***********************************************************************
// <copyright file="ReadOccToDocCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright ©  2020
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using System.Linq;
using Occ.TopAbs;
using Occ.TopoDS;
using ZFGK.Addins;
using ZFGK.DocFrame.Primaries;
using ZFGK.DocFrame.Utility;
using ZFGK.OCC.Utility;
using ZFGK.OSG.DocUI;
using ZFGK.OSG.DocUI.CmdEnables;
using ZFGK.OsgOcc.Doc.CustomEntity;
using ZFGK.Utility;
using ZFGK.WinForms.Utility;

namespace OSS.Doc.Cmds
{
    /// <summary>
    /// Class ReadOccToDocCmd.
    /// Implements the <see cref="ZFGK.Addins.IExtendCommand" />
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    [OnlyDocCmdEnable]
    public class ReadOccToDocCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var docViewForm = cmdData.ViewForm as IViewForm;
            if (docViewForm == null) return CmdResult.Cancel;

            var osgViewCtrl = docViewForm.View as ZfOsgDocViewCtrl;
            var actionUtility = osgViewCtrl.ActionUtility;
            var activeDocument = osgViewCtrl.ActiveDocument;

            // 选择stp文件
            string stpFilePath;
            if (!DialogUtil.Open("选择要导入的STP文件", "STP文件(*.stp,*.step)|*.stp;*.step", out stpFilePath))
            {
                return CmdResult.Cancel;
            }

            var shape = OccImportExportUtil.LoadStp(stpFilePath);
            if (shape == null)
            {
                ZfMessageUtil.ShowError("导入STP文件失败！");
                return CmdResult.Cancel;
            }

            var shapeType = shape.ShapeType;
            var solids = shapeType == ShapeEnum.TopAbs_SOLID ? new[] { new Solid(shape) } : shape.GetEnumerable<Solid>().ToArray();
            if (solids == null || solids.Length == 0)
            {
                ZfMessageUtil.ShowError("导入STP文件失败！");
                return CmdResult.Cancel;
            }

            var ents = new List<ZfFigure>();
            foreach (var solid in solids)
            {
                var ent = new ZfOsgOccEntity();
                ent.Shape = new Shape(solid);
                ents.Add(ent);
            }

            activeDocument.PostToModelSpace(ents, activeDocument.ActiveLayer);

            return CmdResult.Succeed;
        }
    }
}
